package com.nna.videoplayer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.util.Log;

class DemoRenderer extends GLSurfaceView_SDL.Renderer {
	private static int queueSizeTable[] = { 50, 100, 150, 200, 256, 380, 500, 620, 750, 870, 
		1024, 1500, 2048, 3000, 4000, 5000, 8000, 10000, 12000, 14000, 15000, 20000 }; //0-21


	private static final int AV_SYNC_TYPE_AUDIO=0;
	private static final int AV_SYNC_TYPE_VIDEO=1;
	private static final int AV_SYNC_TYPE_EXTERNAL=2;
	
	private static final int AV_FFMPEG_SWS_BICUBIC =0;
	private static final int AV_FFMPEG_SWS_BILINEAR=1;
	private static final int AV_FFMPEG_SWS_FAST_BILINEAR=2;
	
	private static int    skipFrames                = 0;
	private static int    skipBidirFrames           = 0;
	private static int    rgb565                    = 1;
	private static int    yuvRgbAsm                 = 0;  
	
	private	static int    queueSizeMin              = (50 * 1024);
	private static int    queueSizeMax              = (3000 * 1024);
	private static int    queueSizeTotal            = (25000 * 1024);
	private static int    queueSizeAudio            = (512 * 1024);
	
	private	static int    streamqueueSizeMin              = (50 * 1024);
	private static int    streamqueueSizeMax              = (3000 * 1024);
	private static int    streamqueueSizeTotal            = (25000 * 1024);
	private static int    streamqueueSizeAudio            = (512 * 1024);
	
	public static int    fastMode = 0;
	public static int    debugMode= 1;
	public static int    debugVideoMode = 1; //If set to 1, NativeVideoPlayer.java is called for viewing video files
	public static int    syncType = AV_SYNC_TYPE_AUDIO;
	public static int    seekDuration = 0;
	public static int    ffmpegFlags = AV_FFMPEG_SWS_FAST_BILINEAR;
	private Activity context = null;
	public Boolean usergeneratedexitApp 	 = false;
	public Boolean playnextfileFromDirectory = true;
	String         nextFile 				 = Globals.fileName;
	public boolean fileInfoUpdated			 = false;
	private int    loopselected              = 0;
	
	private native void nativeResize(int w, int h);
	public native int nativePlayerTotalDuration();
	private native void nativeInitJavaCallbacks();
	private native int nativePlayerInit(String fileName, int subtitleShow, int subtitleFontSize, int subtitleEncodingType, int rgb565);
	private native int nativeVideoPlayerInit(String fileName, int subtitleShow, int subtitleFontSize, int subtitleEncodingType, int rgb565);
	private native int nativeVideoPlayerExit();
	private native int nativePlayerExit();
	private native int nativePlayerMain(String fileName, int loop,int audioFileType, 
			int skipFrames, int rgb565, int yuvRgbAsm, int skipBidirFrames, int queueSizeMin, int queueSizeMax, int queueSizeTotal, int queueSizeAudio,
			int fastMode, int debugMode, int syncType, int seekDuration, int ffmpegFlags);
	
	DemoRenderer(Activity _context)
	{
		log("DemoRenderer instance created:");
		context = _context;
	}
	
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		log("Surface Created");
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		log("OnSurfaceChanged");
		//nativeResize(width, height);
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		log("Inside on DrawFrame");
		nativeInitJavaCallbacks();
		// Make main thread priority lower so audio thread won't get underrun
		if (Globals.getNativeVideoPlayer()) {
			nativeVideoPlayerInit("/sdcard/broov.ttf",1,11,0, 1);
		}else {
			nativePlayerInit("/sdcard/broov.ttf", 1,11, 0, 1);
		}

		log("Player Filename:"+Globals.fileName);
		
		// Mode to play
		System.out.println("PLAY_ONCE");
		playnextfileFromDirectory = false;
		loopselected = 0;
		
		// Is audio
		int audioFileType;
		audioFileType = 0;
		log("nativePlayerMain(NewPlayer.fileName:"+Globals.fileName+", loopselected:"+loopselected+", audioFileType: "+audioFileType+");");
		////101 - Next button  100 - Previous button  0 - Song played finished

		int retValue;
		
		retValue = nativePlayerMain(Globals.fileName, loopselected, audioFileType, 
				skipFrames, rgb565, yuvRgbAsm, skipBidirFrames, queueSizeMin, queueSizeMax, queueSizeTotal, queueSizeAudio,
				fastMode, debugMode, syncType, seekDuration, ffmpegFlags);
		//Initializing the arraylist
		//clear the array of already played items 
		
		log("Exited after nativePlayerMain");
		if (Globals.getNativeVideoPlayer()) {
			nativeVideoPlayerExit();

		} else {
			nativePlayerExit();
		}
		log("Exited after nativePlayerExit");

		context.finish();
	}
	private void log(String ss){
	    Log.d("nna-videoplayer",ss);
	}
}
